package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业产业比较劳动生产率指标计算类.
 */
public class MajorMarineIndustryCompareWorkProductivityCalculateService extends IndicatorEvaluationCalculateService {

	private String addValueOfMajorMarineIndustryName = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = Double.valueOf(0);
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
			result = calculateResult(indicator, indicatorAccountVO);
		}
		return result;

	}

	/**
	 * 计算H指数
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double calculateResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double addValueOfIndustry = getAddValueOfIndustry(indicatorAccountVO, indicator);

		Double addValueOfMajorMarineIndustry = getAddValueOfMajorMarineIndustry(indicatorAccountVO);

		Double empOfIndustry = getEmpOfIndustry(indicatorAccountVO, indicator);

		Double empOfMajorMarineIndustry = getEmpOfMajorMarineIndustry(indicatorAccountVO);

		// if (Double.valueOf("0").equals(addValueOfMajorMarineIndustry)) {
		// return 0d;
		// }
		Validate.isTrue(!(Double.valueOf(0).equals(addValueOfMajorMarineIndustry)));
		Validate.isTrue(!(Double.valueOf(0).equals(empOfMajorMarineIndustry)));
		// if (Double.valueOf("0").equals(empOfMajorMarineIndustry)) {
		// return 0d;
		// }

		return (addValueOfIndustry / empOfIndustry) / (addValueOfMajorMarineIndustry / empOfMajorMarineIndustry);
	}

	/**
	 * 获得省级十个主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得省级主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfMajorMarineIndustry(IndicatorAccountVO indicatorAccountVO) {

		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				addValueOfMajorMarineIndustryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得省级十个主要海洋产业的劳动力人数
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getEmpOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_MAP.get(indicator
				.getIndustryCode());
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				industryName + "劳动力人数");
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得省级十个主要海洋产业的劳动力人数之和
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getEmpOfMajorMarineIndustry(IndicatorAccountVO indicatorAccountVO) {
		Double result = 0d;
		for (String industryName : ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_MAP.values()) {
			Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
					indicatorAccountVO.getYear(),
					industryName + "劳动力人数");
			if (inputOrAccountingIndicatorValue == null) {
				continue;
			}
			result += inputOrAccountingIndicatorValue;
		}
		return result;
	}

}
